package com.fh.common.distributedlock.redis;

import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.core.RedisTemplate;

import com.fh.common.distributedlock.DistributeLock;

/**
 * @author Qiu,Junda 
 * @date 2019年5月26日
 * @description
 */
public class ClusterRedisDistributeLock implements DistributeLock{

	private RedisTemplate<String, Object> redisTemplate;
	
	private final String DEFAULT_VALUE="1";
	
	public ClusterRedisDistributeLock(RedisTemplate<String, Object> redisTemplate) {
		this.redisTemplate=redisTemplate;
	}
	
	@Override
	public boolean lock(String lockKey,int expireSeconds) {
		return redisTemplate.opsForValue().setIfAbsent(lockKey, DEFAULT_VALUE, expireSeconds,TimeUnit.SECONDS);
	}

	@Override
	public boolean unLock(String lockKey) {
		return redisTemplate.delete(lockKey);
	}

}
